/*  ----------------------------------------------------------------------------
 *  Arello Mobile
 *  Fast 3d Engine
 *  ----------------------------------------------------------------------------
 * Except where otherwise noted, this work is licensed under
 * http://creativecommons.org/licenses/by/3.0
 */

#include "Hardware.h"
#include "Creator.h"
#include <Gluck.h>

#ifdef IPHONE_VERSION
#include <noise/MusicChangerIphone.h>
#import <AudioToolbox/AudioServices.h>
#endif

namespace liba
{
namespace noise
{
namespace hard
{
namespace openal
{
	
static Hardware * sound_hardware = 0;

#ifdef IPHONE_VERSION
static MusicChanger * music_changer = 0;

static bool gOtherAudioIsPlaying = true;
static bool audiosession_initialized = false;

void CheckIfOtherAudioIsPlaying(void)
{
	UInt32		propertySize, audioIsAlreadyPlaying;
	
	if(!audiosession_initialized)
	{
		AudioSessionInitialize(NULL,NULL,NULL,NULL);
		audiosession_initialized = true;
	}
	
	// do not open the track if the audio hardware is already in use (could be the iPod app playing music)
	propertySize = sizeof(UInt32);
	AudioSessionGetProperty(kAudioSessionProperty_OtherAudioIsPlaying, &propertySize, &audioIsAlreadyPlaying);
	if (audioIsAlreadyPlaying != 0)
	{
		gOtherAudioIsPlaying = true;
		//NSLog(@"other audio is playing");
		
		UInt32	sessionCategory = kAudioSessionCategory_AmbientSound;
		AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);
		AudioSessionSetActive(true);
	}
	else
	{
		//NSLog(@"no other audio is playing ...");
		
		gOtherAudioIsPlaying = false;
		
		// since no other audio is *supposedly* playing, then we will make darn sure by changing the audio session category temporarily
		// to kick any system remnants out of hardware (iTunes (or the iPod App, or whatever you wanna call it) sticks around)
		UInt32	sessionCategory = kAudioSessionCategory_MediaPlayback;
		AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);
		AudioSessionSetActive(true);
		
		// now change back to ambient session category so our app honors the "silent switch"
		sessionCategory = kAudioSessionCategory_SoloAmbientSound;//kAudioSessionCategory_AmbientSound;
		AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);
	}
}
#endif
	
Creator::Creator()
{
	if( sound_hardware )
		THROW(Gluck(__FUNCTION__, "Hardware already exists"));

#ifdef IPHONE_VERSION
	CheckIfOtherAudioIsPlaying();
#endif
	
	sound_hardware = new Hardware();
}

Creator::~Creator()
{
	delete sound_hardware; sound_hardware = 0;
}

void Creator::life_cycle(double delta_time)
{
	if( sound_hardware )
		sound_hardware->life_cycle( delta_time );
}

#ifdef IPHONE_VERSION
void Creator::register_music_changer(MusicChanger * mc)
{
	music_changer = mc;
}
	
bool Creator::can_play_music()
{
	return !gOtherAudioIsPlaying;
}

static bool not_active = false;
void Creator::app_goes_background()
{
	not_active = true;
	
	if(music_changer)
		music_changer->app_goes_background();

	if(sound_hardware)
		sound_hardware->app_goes_background();
	
	AudioSessionSetActive(false);
}
	
void Creator::app_goes_active()
{
	if(!not_active)
		return;
	
	// the user might have double-clicked home and started playing music, so do the same as at app launch
	CheckIfOtherAudioIsPlaying();

	if(music_changer)
		music_changer->app_goes_active();
	
	if(sound_hardware)
		sound_hardware->app_goes_active();
}

#endif

} // namespace openal
} // namespace hard
} // namespace noise
} // namespace liba


